{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "0    91\n1    72\n2    84\ndtype: int32"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "0    62\n1    74\n2    55\n3    59\ndtype: int32"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "s1 = Series(np.random.randint(10, 100, size=3))\n",
    "s2 = Series(np.random.randint(10, 100, size=4))\n",
    "display(s1 , s2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "0    153.0\n1    146.0\n2    139.0\n3      NaN\ndtype: float64"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1+s2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 1, 2])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([0, 1, 2, 3])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n1 = np.arange(3)\n",
    "n2 = np.arange(4)\n",
    "display(n1 , n2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (3,) (4,) ",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[7], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43mn1\u001B[49m\u001B[38;5;241;43m+\u001B[39;49m\u001B[43mn2\u001B[49m\n",
      "\u001B[1;31mValueError\u001B[0m: operands could not be broadcast together with shapes (3,) (4,) "
     ]
    }
   ],
   "source": [
    "n1+n2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[8, 4, 6]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([[4],\n       [7],\n       [2]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([[12,  8, 10],\n       [15, 11, 13],\n       [10,  6,  8]])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 只有1行或者1列的时候，才能用于广播\n",
    "n1 = np.array(np.random.randint(1, 10, size=(1, 3)))\n",
    "n2 = np.array(np.random.randint(1, 10, size=(3, 1)))\n",
    "display(n1 ,n2)\n",
    "n1+n2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "0    16\n1    69\n2    50\ndtype: int32"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "a    18\nb    59\nc    89\nd    49\ndtype: int32"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "s3 = Series(np.random.randint(10, 100, size=3),index=range(3))\n",
    "s4 = Series(np.random.randint(10, 100, size=4),index=[\"a\",\"b\",\"c\",\"d\"])\n",
    "display(s3 , s4)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "0   NaN\n1   NaN\n2   NaN\na   NaN\nb   NaN\nc   NaN\nd   NaN\ndtype: float64"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3+s4\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "0    16.0\n1    69.0\n2    50.0\na    18.0\nb    59.0\nc    89.0\nd    49.0\ndtype: float64"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3.add(s4,fill_value=0)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}